//
//  TCLYWithdrawalViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/9/26.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYWithdrawalViewController.h"
#import "TCLYMineModel.h"
#import "TCLYRegisterView.h"
#import "TCLYUser.h"
#import "Macros.h"
#import "Masonry.h"

@interface TCLYWithdrawalViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *moneyTextField;
@property (weak, nonatomic) IBOutlet UILabel *balanceLabel;
@property (weak, nonatomic) IBOutlet UIButton *withdrawalBtn;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewHeight;
@property (weak, nonatomic) IBOutlet UIScrollView *bgScrollView;
@property (nonatomic, strong) TCLYMineModel * mineModel;
@property (nonatomic, strong) TCLYUser * user;
@property (nonatomic, strong) TCLYRegisterView * promView;

@end

@implementation TCLYWithdrawalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UITapGestureRecognizer * textFeildTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:textFeildTap];
    
    self.view.backgroundColor = bgColor;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(withdrawalSucceed) name:@"withdrawalSucceed" object:nil];
    self.mineModel = [[TCLYMineModel alloc] init];
    self.promView = [[TCLYRegisterView alloc] init];
    self.user = [[TCLYUser alloc] init];
    [self createTitleView];
    [self createView];
    if (screenHeight >= 667.0) {
        self.bgViewHeight.constant = 300;
    }else{
        self.bgViewHeight.constant = 667.0;
    }
}
-(void)createView{
    self.balanceLabel.text = [NSString stringWithFormat:@"当前钱包余额 ¥%@",self.moneyStr];
    self.bgView.backgroundColor = bgColor;
    UIButton * allMoneyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [allMoneyBtn setTitle:@"提现" forState:UIControlStateNormal];
    [allMoneyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    allMoneyBtn.backgroundColor = btnColor;
    allMoneyBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    allMoneyBtn.layer.cornerRadius = 5;
    allMoneyBtn.layer.masksToBounds = YES;
    [allMoneyBtn addTarget:self action:@selector(allMoneyBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:allMoneyBtn];
    [allMoneyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgScrollView.mas_bottom);
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(46);
    }];
}
-(IBAction)withdrawalBtnAction:(id)sender{
    if ([self.nameTextField.text isEqual:@""]) {
        [self.promView prompt:@"请填写真实姓名" Controller:self];
        return;
    }
    if ([self.accountTextField.text isEqual:@""] && self.accountTextField.text.length == 11) {
        [self.promView prompt:@"请认真填写账号哦!\n支付宝账号一般为手机号哦!" Controller:self];
        return;
    }
    double money;
    money = [self.moneyStr doubleValue];
    if (self.moneyStr == 0 ) {
        [self.promView prompt:@"您的钱包里没有余额哦" Controller:self];
        return;
    }
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", self.nameTextField.text,@"username",self.moneyStr,@"cash", self.accountTextField.text,@"account",nil];
    [self.mineModel withdrawalParameter:dic];
}
-(void)allMoneyBtnAction{
    if ([self.nameTextField.text isEqual:@""]) {
        [self.promView prompt:@"请填写真实姓名" Controller:self];
        return;
    }
    if ([self.accountTextField.text isEqual:@""] && self.accountTextField.text.length == 11) {
        [self.promView prompt:@"请认真填写账号哦!\n支付宝账号一般为手机号哦!" Controller:self];
        return;
    }
    if ([self.moneyTextField.text isEqual:@""]) {
        [self.promView prompt:@"请填写您的提现金额" Controller:self];
        return;
    }
    double money;
    money = [self.moneyStr doubleValue];
    double withdrawal;
    withdrawal = [self.moneyTextField.text doubleValue];
    
    if (withdrawal > money) {
        [self.promView prompt:@"您的钱包里没有这么多的余额哦" Controller:self];
        return;
    }
    if (withdrawal <= 0) {
        [self.promView prompt:@"请填写正确的提现金额" Controller:self];
        return;
    }
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", self.nameTextField.text,@"username",self.moneyTextField.text,@"cash", self.accountTextField.text,@"account",nil];
    [self.mineModel withdrawalParameter:dic];
}
-(void)withdrawalSucceed{
    if ([self.mineModel.withdrawalSucceed isEqual:@1]) {
        [self.promView Prompt:@"申请提现成功,24小时之内到账" Controller:self];
        self.promView.determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popToRootViewControllerAnimated:YES];
            return ;
        }];
        [self.promView.promptAlertCtr addAction:self.promView.determineAction];
    }else{
        [self.promView Prompt:@"系统正忙，请您稍后再试" Controller:self];
        self.promView.determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popToRootViewControllerAnimated:YES];
            return ;
        }];
        [self.promView.promptAlertCtr addAction:self.promView.determineAction];
    }
}
-(void)createTitleView{
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    self.navigationController.navigationBar.backgroundColor = statusColor;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"提现";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}
-(void)dismissKeyboard{
    [self.nameTextField resignFirstResponder];
    [self.accountTextField resignFirstResponder];
    [self.moneyTextField resignFirstResponder];
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = NO;
            self.tabBarController.tabBar.hidden = YES;
        }
            break;
        case 92:{
            
        }
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
